﻿//------------------------------------------------------------------------------------------------- 
// <copyright file="EnumValuesCache.cs" company="Christopher Hines">
// Copyright (c) Christopher Hines. All rights reserved.
// </copyright>
// <summary>
//    Provides cache for enums and their values
// </summary>
//-------------------------------------------------------------------------------------------------

namespace Copperfield
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Provides cache for enums and their values
    /// </summary>
    internal static class EnumValuesCache
    {
        /// <summary>
        /// The enum values cache collection
        /// </summary>
        private static readonly Dictionary<Type, List<object>> cache = new Dictionary<Type, List<object>>();

        /// <summary>
        /// Returns the values associated with a specified enumerated type
        /// </summary>
        /// <param name="enumType">The type of enumerated type for which values should be retrieved</param>
        /// <returns>The values of the specified enumerated type</returns>
        public static List<object> GetValues(Type enumType)
        {
            if (!enumType.IsEnum)
            {
                return new List<object>();
            }

            if (!cache.ContainsKey(enumType))
            {
                cache.Add(enumType, Enum.GetValues(enumType).Cast<object>().ToList());
            }

            return cache[enumType];
        }
    }
}
